package serv_impl

import "time"

func (apiImpl *NetProxyApiImpl) cleanTunnel() {
	cmpTime := time.Now().Unix() - 30
	removeItemList := []*TunnelItem{}
	apiImpl.tunnelMap.Range(func(_, value any) bool {
		realValue := value.(*TunnelItem)
		if realValue.LastReadTime < cmpTime {
			removeItemList = append(removeItemList, realValue)
		}
		return true
	})

	for _, removeItem := range removeItemList {
		apiImpl.tunnelMap.Delete(removeItem.TunnelId)
	}
}

func (apiImpl *NetProxyApiImpl) cleanConn() {
	cmpTime := time.Now().Unix() - 30
	removeItemList := []*ConnItem{}
	apiImpl.connMap.Range(func(_, value any) bool {
		realValue := value.(*ConnItem)
		if realValue.LastReadTime < cmpTime {
			removeItemList = append(removeItemList, realValue)
		}
		return true
	})

	for _, removeItem := range removeItemList {
		apiImpl.connMap.Delete(removeItem.ConnId)
		close(removeItem.OutChan)
		removeItem.TargetConn.Close() //skip error check
	}
}
